package top.cardone.security.shiro.func;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import top.cardone.core.util.func.Func0;
import top.cardone.core.util.func.Func1;

/**
 * Created by yht on 16-3-28.
 */
public class ReadPrincipalFunc implements Func0<String>, Func1<String, Object> {
	@Override
	public String func() {
		Object principal = SecurityUtils.getSubject().getPrincipal();

		if (principal == null) {
			return null;
		}

		return principal.toString();
	}

	@Override
	public String func(Object obj) {
		if (obj instanceof String) {
			return ObjectUtils.defaultIfNull(this.func(), (String) obj);
		}

		return this.func();
	}
}
